<?php 
	include 'php/config.php';
	session_start();
	if (!$_GET["badgeId"]) {
		header("Location: " . $ISSUER_HOST);
		exit();
	}	
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf8" />
<title>Open Badges - Badge Info</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.2/themes/cupertino/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
<script src="js/utils.js"></script>
<script src="js/login.js"></script>
<script>
$(function() {
	$('#loginForm').hide();
	$('a#menuLogin').click(function(evt) {
		evt.preventDefault();
		$('#loginForm').slideToggle(200);
	});
	$('#login').click(function(evt) {
		evt.preventDefault();
		$('#loginForm').slideToggle(200);
	});
	$('input[type="button"]').button();
});
</script>
</head>
<body>
<div id="topPan"><img src="images/logo.gif" alt="Education Zone" width="245" height="37" border="0"  class="logo" title="Education Zone"/>
  <p>Open Badges</p>
  <div id="topContactPan"> </div>
  <div id="topMenuPan">
    <div id="topMenuLeftPan"></div>
    <div id="topMenuMiddlePan">
      <ul>
        <li><a href="index.php">Home</a></li>
        <li><a href="badges_page.php">Badges</a></li>
        <li><a href="displayer_page.php">Displayer</a></li>
        <li><a href="universities.php">Università</a></li>
        <li><a href="refs.php">Riferimenti</a></li>
        <li><a href="contacts.php">Contatti</a></li>
        <?php
		if (!$_SESSION['id_utente']) {
    	?>
        <li class="contact"><a id="menuLogin" class="contact" href="login.php">Login</a></li>
	<?php
		} else {
	?>
	<li class="contact"><a class="contact" href="logout.php">Logout</a></li>
	<?php
		}
	?>
      </ul>
    </div>
    <div id="topMenuRightPan"></div>
  </div>
</div>
<div id="bodyPan">
  <div id="bodyLeftPan">
    <h2><span>Open Badges - Badge Info</span></h2>
    <p id="message" class="errorMsg"></p>
    <?php
	// Connecting, selecting database
	$mysqli = new mysqli($MYSQL_HOST, $MYSQL_USERNAME, $MYSQL_PASSWORD, $MYSQL_DB_NAME);
	if ($mysqli->connect_errno) {
		echo "<p>Failed to connect to MySQL: " . $mysqli->connect_error . "</p>";
	} else {
		$mysqli->set_charset("utf8");
		$query = sprintf($BADGE_QUERY, $_GET["badgeId"]);
		$result = $mysqli->query($query);
		if ($result) {
			if ($result->num_rows) {
				$row = $result->fetch_assoc();
				echo "<div>";
				echo "<h3>" . $row['badgeName'] . "</h3>";
				echo "<img src=\"" . $row['immagine'] . "\"  height=\"150\" width=\"150\">";
				echo "<p>" . $row['descrizione'] . "</p>";
				if ($row['tags']) {
					echo "<p>Tags: " . $row['tags'] . "</p>";
				}
				if ($row["condivisa"]) {
					echo "<p>La badge è rilasciata da tutte le università</p>";
				} else {
					echo "<p>La badge è rilasciata da " . $row["issuerName"] . "</p>";
				}
				echo "</div>";
				$query = sprintf($GET_BADGE_CRITERIA, $_GET["badgeId"]);
				$criteria = $mysqli->query($query);
				if ($criteria) {
					if ($criteria->num_rows) {
						echo "<table>";
						while ($cRow = $criteria->fetch_assoc()) {
							echo "<tr>";
							echo "<td class=\"criteriaType\">" . $cRow["type"] . "</td>";
							echo "<td>" . $cRow["content"] . "</td>";
							echo "</tr>";
						}
						echo "</table>";
					}
				} else {
					echo "<p>Errore nel recupero dei criteri associati alla badge</p>";
					echo "<p>Failed to run query '" . $query . "': (" . $mysqli->errno . ") " .
							$mysqli->error . "</p>";
				}
				mysqli_free_result($criteria);
			} else {
				echo "<p>La badge richiesta non esiste</p>";
			}				
				
		} else {
			echo "<p>Failed to run query '" . $query . "': (" . $mysqli->errno . ") " .
				$mysqli->error . "</p>";
		}
		
		mysqli_free_result($result);
		$mysqli->close();		
	}
	?>
  </div>
  <div id="bodyRightPan">
    <?php
	if (!$_SESSION['id_utente']) {
    ?>
    <h2 id="login"><span>Log</span>in</h2>
    <form id="loginForm">
    <p class="boldtext">Username</p>
    <p><input id="username" name="username" type="text"/></p>
    <p id="userMsg" class="errorMsg"></p>
    <p class="boldtext">Password</p>
    <p><input id="password" name="password" type="password"/></p>
    <p id="pswMsg" class="errorMsg"></p>
    <p><input type="button" value="Login" onclick="validateLoginForm()"/></p>
    </form>
    <?php
	}
    ?>
    <h2><span>Quick</span> links</h2>
    <ul>
      <li><a href="index.php">Home</a> </li>
      <?php
	if ($_SESSION['id_utente']) {
		if ($_SESSION['admin']) {
      ?>
      <li><a href="issue_page.php">Assegna badge</a> </li>
      <li><a href="new_badge_page.php">Nuova badge</a> </li>
      <li><a href="categories_page.php">Gestisci categorie</a> </li>
      <li><a href="new_user_page.php">Nuovo utente</a> </li>
      <?php
		} else {
      ?>
      <li><a href="personal_page.php">Pagina personale</a> </li>
      <?php
		}
      ?> 
      <li><a href="badges_page.php">Badges</a> </li>
      <li><a href="displayer_page.php">Displayer</a> </li>     
      <li><a href="change_password_page.php">Cambia password</a> </li>
      <li><a href="logout.php">Logout</a> </li>
      <?php
		} else {
      ?>
      <li><a href="badges_page.php">Badges</a> </li>
      <li><a href="displayer_page.php">Displayer</a> </li>
      <?php
		}
      ?> 
    </ul>
  </div>
</div>
	<div id="footermainPan">
	  <div id="footerPan">
	    <ul>
	      <li><a href="index.php">Home</a>| </li>
	      <li><a href="badges_page.php">Badges</a>| </li>
	      <li><a href="displayer_page.php">Displayer</a>| </li>
      	      <?php if ($_SESSION['id_utente'] && !$_SESSION['admin']) { ?><li><a href="personal_page.php">Pagina personale</a>| </li> <?php } ?>
	      <li><a href="universities.php">Università</a>| </li>
	      <li><a href="refs.php">Riferimenti</a>| </li>
	      <li><a href="contacts.php">Contatti</a></li>
	    </ul>
	    <p class="copyright">©education zone. All right reserved.</p>
	    <ul class="templateworld">
	      <li>design by:</li>
	      <li><a href="http://www.templateworld.com" target="_blank">Template World</a></li>
	    </ul>
	    <div id="footerPanhtml"><a href="http://validator.w3.org/check?uri=referer" target="_blank">HTML</a></div>
	    <div id="footerPancss"><a href="http://jigsaw.w3.org/css-validator/check/referer" target="_blank">css</a></div>
	  </div>
	</div>
</body>
</html>
